#include "rclcpp/rclcpp.hpp"
#include "nav_msg_pkg/srv/add_ints.hpp"

using nav_msg_pkg::srv::AddInts;

class AddIntsServer : public rclcpp::Node
{
public:
    AddIntsServer() : Node("add_ints_server")
    {
        RCLCPP_INFO(this->get_logger(), "add_ints_server 节点已经启动!");
        add_ints_service_ = this->create_service<AddInts>("add_ints", std::bind(&AddIntsServer::add_ints_callback, this, std::placeholders::_1, std::placeholders::_2));
    }

private:
    rclcpp::Service<AddInts>::SharedPtr add_ints_service_;
    void add_ints_callback(const std::shared_ptr<AddInts::Request> request,
                           const std::shared_ptr<AddInts::Response> response)
    {
        response->sum = request->num1 + request->num2;
        RCLCPP_INFO(this->get_logger(), "收到请求: %d + %d", request->num1, request->num2);
        RCLCPP_INFO(this->get_logger(), "发送响应: %d", response->sum);
    }
};

int main(int argc, char *argv[])
{
    /* 初始化rclcpp  */
    rclcpp::init(argc, argv);
    /* 产生一个 节点 */
    auto node = std::make_shared<AddIntsServer>();
    /* 运行节点，并检测退出信号 Ctrl+C */
    rclcpp::spin(node);
    /* 停止运行 */
    rclcpp::shutdown();
    return 0;
}